<?php
  require_once('includes/db.php');

  // Allocate all necessary memory for the image.

    pr1($_POST);
    pr1($_FILES);
  if(isset($_POST['name']))
  {
    $_POST['added_date'] = date('Y-m-d H:i:s');
    $_POST['status'] = '1';

    $allowedSize = 1; //MB
    $allowedFileSize = $allowedSize * 1024 * 1024 ; // Bytes
    $allowedExt = array('jpg','jpeg','png','bmp');
    $extns = implode(',',$allowedExt);


    if($_FILES['image']['error']==0) {

      if($_FILES['image']['size'] <= $allowedFileSize)
      {

        $p_image = $_FILES['image']['name'];
        $p_image = date('ymdHis').'_'.uniqid().'_'.$p_image;
        $fileInfoArray = pathinfo($p_image);
        $fileExt = $fileInfoArray['extension'];

        if(in_array($fileExt,$allowedExt)) {
          $src = $_FILES['image']['tmp_name'];
          $dest = "uploads/products/$p_image";
          $isMoved = @move_uploaded_file($src, $dest);

          $_POST['image'] = $p_image;
          if($isMoved == true) {
            ## Creating Thumbnails for Uploaded Image
            ini_set('memory_limit', '-1');
            // include image processing code
            require_once('thumb/image.class.php');

            $img = new Zubrag_image;
            $img->max_x        = 700;
            $img->max_y        = 150;
            $img->GenerateThumbFile($dest, "uploads/products/thumbs/$p_image");//700/50
            ## End Thumb process

            $id = insert('products',$_POST);
            //echo "<h4>records inserted on id</h4>
                  //<h1>$id</h1>";
            redirect("show_all_products.php");

          } else {
            echo "Uploads folder misssing or there may be write permission issue. ";
          }

        } else {
          echo "Please upload file with valid extn ($extns)";
        }
      } else {
        echo "Please upload file with MAX $allowedSize MB";
      }
    } else {
      echo "Error while Uploading File Size Exceed to 2MB";
    }
  } else {
    // if someone hits this page directly from URL
    redirect("add_product.php");
  }
